package com.google.android.exoplayer2.source;

import androidx.annotation.Nullable;
import java.io.IOException;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import o.abr;
import o.ahv;
import o.aia;
import o.aih;
import o.aim;
import o.aim.a;
import o.aiv;
import o.alc;
import o.alx;

public final class MergingMediaSource extends ahv<Integer> {
    private int ʻ;
    private final aia ˊ;
    private Object ˋ;
    private final ArrayList<aim> ˎ;
    private final aim[] ˏ;
    private final abr[] ॱ;
    private IllegalMergeException ᐝ;

    public static final class IllegalMergeException extends IOException {
        public final int ˋ;

        @Documented
        @Retention(RetentionPolicy.SOURCE)
        public @interface Reason {
        }

        public IllegalMergeException(int i) {
            this.ˋ = i;
        }
    }

    public /* synthetic */ void ˋ(Object obj, aim o_aim, abr o_abr, @Nullable Object obj2) {
        ˎ((Integer) obj, o_aim, o_abr, obj2);
    }

    public void ॱ(@Nullable alx o_alx) {
        super.ॱ(o_alx);
        for (int i = 0; i < this.ˏ.length; i++) {
            ˊ((Object) Integer.valueOf(i), this.ˏ[i]);
        }
    }

    public void ॱ() throws IOException {
        if (this.ᐝ != null) {
            throw this.ᐝ;
        }
        super.ॱ();
    }

    public aih ˊ(a aVar, alc o_alc, long j) {
        int i = 0;
        aih[] o_aihArr = new aih[this.ˏ.length];
        int ˎ = this.ॱ[0].ˎ(aVar.ˋ);
        while (i < o_aihArr.length) {
            o_aihArr[i] = this.ˏ[i].ˊ(aVar.ˋ(this.ॱ[i].ˋ(ˎ)), o_alc, j);
            i++;
        }
        return new aiv(this.ˊ, o_aihArr);
    }

    public void ॱ(aih o_aih) {
        aiv o_aiv = (aiv) o_aih;
        for (int i = 0; i < this.ˏ.length; i++) {
            this.ˏ[i].ॱ(o_aiv.ˊ[i]);
        }
    }

    public void ˎ() {
        super.ˎ();
        Arrays.fill(this.ॱ, null);
        this.ˋ = null;
        this.ʻ = -1;
        this.ᐝ = null;
        this.ˎ.clear();
        Collections.addAll(this.ˎ, this.ˏ);
    }

    protected void ˎ(Integer num, aim o_aim, abr o_abr, @Nullable Object obj) {
        if (this.ᐝ == null) {
            this.ᐝ = ˎ(o_abr);
        }
        if (this.ᐝ == null) {
            this.ˎ.remove(o_aim);
            this.ॱ[num.intValue()] = o_abr;
            if (o_aim == this.ˏ[0]) {
                this.ˋ = obj;
            }
            if (this.ˎ.isEmpty()) {
                ˏ(this.ॱ[0], this.ˋ);
            }
        }
    }

    @Nullable
    protected a ˊ(Integer num, a aVar) {
        return num.intValue() == 0 ? aVar : null;
    }

    private IllegalMergeException ˎ(abr o_abr) {
        if (this.ʻ == -1) {
            this.ʻ = o_abr.ˎ();
        } else if (o_abr.ˎ() != this.ʻ) {
            return new IllegalMergeException(0);
        }
        return null;
    }
}
